<template>
	<view class="addPost">
		<div class="top">
			<span class="iconfont icon-zuojiantou" @click="go"></span>
			<span>新增帖子</span>
			<span>发布</span>
		</div>
		<u-popup :show="show" @close="close" @open="open" closeable="true">
			
			
			
			
			<scroll-view scroll-y="true" class="scroll-item">
				 <view v-for="(item,index) in list" :key="index" class="center" @click="add(item)">
		            <text class="center-item">{{item.title}}</text>
		            </view>
			</scroll-view>
			
			
			
			
			
		</u-popup>
		<u-picker :show="show" :columns="list" @confirm="confirm1" @cancel="cancel"></u-picker>
		<u-button @click="show = true">{{title}}</u-button>
		<u--textarea v-model="value1" placeholder="请输入内容" class="textaea"></u--textarea>
		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="10" ></u-upload>
	</view>
</template>

<script>
	import{getbbs} from '../request/index.js'
	export default {
		data() {
			return {
				query:{
					page: 1,
					limit: 100
				},
				 value: 0,
				 show: false,
				list: [],
				title:'选择社区',
				value1: '',
				fileList1:[]
			};
		},
		methods:{
			
			add(index){
				if(index==2){
					跳转
				}
			},
			
			
			go(){
				uni.navigateBack(1)
			},
			bindClick(e){
				
			},
			confirm1(e){
				this.title=e.value[0]
				this.show = false
			},
			cancel(){
				this.show = false
			},
			deletePic(event) {
							this[`fileList${event.name}`].splice(event.index, 1)
						},
			async afterRead(event) {
							// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
							let lists = [].concat(event.file)
							let fileListLen = this[`fileList${event.name}`].length
							lists.map((item) => {
								this[`fileList${event.name}`].push({
									...item,
									status: 'uploading',
									message: '上传中'
								})
							})
							for (let i = 0; i < lists.length; i++) {
								const result = await this.uploadFilePromise(lists[i].url)
								let item = this[`fileList${event.name}`][fileListLen]
								this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
									status: 'success',
									message: '',
									url: result
								}))
								fileListLen++
							}
						},
						uploadFilePromise(url) {
							return new Promise((resolve, reject) => {
								let a = uni.uploadFile({
									url: 'http://demonuxtapi.dishait.cn/mobile/upload', 
									filePath: url,
									name: 'file',
									header:{
										appid:'bd9d01ecc75dbbaaefce'
									},
									formData: {
										user: 'test'
									},
									success: (res) => {
										setTimeout(() => {
											resolve(res.data.data)
										}, 1000)
									}
								});
							})
						},
			
		},
		onLoad() {
			getbbs(this.query).then(res=>{
				let aa=[]
				res.data.data.rows.forEach(item=>{
					aa.push(item.title)
				})
				this.list=[aa]
			})
		}
	}
</script>

<style lang="scss">
.addPost{
	width: 100%;
	height: 100%;
	.top{
		width: 100%;
		height: 50px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		box-sizing: border-box;
		padding: 0 10px;
	}
	.scroll-item{
		max-height: 300px;
		width: 100%;
		.center{
			width: 100%;
			text-align: center;
			margin-top: 10px;
		}
	}
	.textaea{
		margin: 10px 0;
	}
}
</style>
